package cn.sciento.farm.devicemanager.quartz.trigger;


import cn.sciento.farm.devicemanager.common.constant.DeviceConstant;
import cn.sciento.farm.devicemanager.common.constant.ResultConstant;
import cn.sciento.farm.devicemanager.common.util.SpringContextHolder;
import cn.sciento.farm.devicemanager.dao.entity.DevController;
import cn.sciento.farm.devicemanager.dao.entity.DevControllerLog;
import cn.sciento.farm.devicemanager.dao.entity.DevTimer;
import cn.sciento.farm.devicemanager.dao.entity.DevTimerLog;
import cn.sciento.farm.devicemanager.gateway.downlink.DelayManager;
import cn.sciento.farm.devicemanager.gateway.downlink.MqttDataFactory;
import cn.sciento.farm.devicemanager.gateway.downlink.service.MqttBlockedSendManager;
import cn.sciento.farm.devicemanager.gateway.model.downlink.BaseBlockControlData;
import cn.sciento.farm.devicemanager.gateway.uplink.service.base.LogTaskFactory;
import cn.sciento.farm.devicemanager.quartz.SwitchJobContent;
import cn.sciento.farm.devicemanager.service.ControllerService;
import cn.sciento.farm.devicemanager.service.DeviceControlService;
import cn.sciento.farm.devicemanager.service.TimerService;
import com.alibaba.fastjson.JSON;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author wumu
 * @version V1.0
 * @date 4/12/18
 * @time 5:09 PM
 */
public class CollectionTrigger {
    private static Logger logger = LoggerFactory.getLogger(CollectionTrigger.class);

    private static MqttBlockedSendManager blockedSendManager = SpringContextHolder.getBean(MqttBlockedSendManager.class);

    private static TimerService timerService = SpringContextHolder.getBean(TimerService.class);

    private static ControllerService controllerService = SpringContextHolder.getBean(ControllerService.class);
    private static DeviceControlService deviceControlService = SpringContextHolder.getBean(DeviceControlService.class);

    public static void trigger(JobExecutionContext jobExecutionContext) {
        if (blockedSendManager == null) {
            logger.info("空异常");
            return;
        }
        JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
        logger.info("触发定时任务：" + JSON.toJSONString(jobDataMap));
        String params = jobDataMap.getString("params");
        SwitchJobContent switchJobContent = JSON.parseObject(params, SwitchJobContent.class);
        if (switchJobContent == null){
            return;
        }
        DevController controller = controllerService.getById(switchJobContent.getControllerId());
        if(controller == null){
            return;
        }
        DevTimer devTimer = timerService.getById(switchJobContent.getTimerId());
        // 判断定时任务存在
        if(devTimer == null){
            return;
        }
        if (devTimer.getStatus() == 0) {
            return;
        }
        int action = devTimer.getAction();
        int duration = devTimer.getDuration();
        long time = System.currentTimeMillis();
        controller.setDuration(duration);
        int count = deviceControlService.sendBlockControl(controller.getId(), action, duration);

        DevControllerLog switchLog = new DevControllerLog();
        // 储存开关日志
        switchLog.setControllerId(controller.getId());
        switchLog.setName(controller.getName());
        switchLog.setTenantId(controller.getTenantId());
        switchLog.setResult(count);
        switchLog.setTimerId(devTimer.getId());

        switchLog.setAction(action);
        switchLog.setDuration(duration);
        switchLog.setStartTime(time);
        switchLog.setType(DeviceConstant.TIMER_CONTROL);
        DelayManager.getInstance().executeLog(LogTaskFactory.saveControllerLog(switchLog));

        // 储存定时触发的日志
        DevTimerLog switchTimerLog = new DevTimerLog();
        switchTimerLog.setResult(count);
        switchTimerLog.setStartTime(time);
        switchTimerLog.setControllerId(controller.getId());
        switchTimerLog.setTimerId(devTimer.getId());
        switchTimerLog.setName(controller.getName());
        switchTimerLog.setTenantId(controller.getTenantId());
        DelayManager.getInstance().executeLog(LogTaskFactory.saveTimerLog(switchTimerLog));
    }



}
